
var $tabs = null;

$(function(){
	$tabs = $('#frames').tabs({
		fit:true,border:false,pill:true,tools:[{id:'btnMinMax',iconCls:'panel-tool-max',handler:maxMin},{iconCls:'panel-tool-close',handler:closeAll}]
	
	});
	$tabs.tabs('add',{
		title:'桌面',iconCls:'icon-home',content:'<iframe class="frame" src="desktop"></iframe>'
	});
	/*$.post('menu.do',function(roots) {
		var html = '', root, children = [];
		for(var i = 0; i < roots.length; i++) {
			root = roots[i];
			if(root.children && root.children.length > 0) {
				children[children.length] = root.children;
				html += '<div title="'+root.text+'" data-options="iconCls:\''+root.iconCls+'\'"><ul></ul></div>';	
			}
		}
		$('#menus').html(html).accordion({animate:true,multiple:false,border:false}).find('ul').each(function(i) {
			$(this).tree({data:children[i],animate:true,onClick:openTab});
		});
	});*/
	$('#menus').accordion({animate:true,multiple:false,border:false}).find('ul').each(function(i) {
		$(this).tree({animate:true,onClick:openTab});
	});
	$('#frames').on('dblclick','.tabs-inner',function() {
		maxMin();
	});
});

function maxMin() {
	var o = $('#btnMinMax');
	var isMax = o.linkbutton('options').iconCls == 'panel-tool-restore';
	var $body = $('body');
	if(isMax) {
		$body.layout('expand','north');
		$body.layout('expand','west');
		o.linkbutton({iconCls:'panel-tool-max'});
	}
	else {
		$body.layout('collapse','north');
		$body.layout('collapse','west');
		o.linkbutton({iconCls:'panel-tool-restore'});
	}
}

function closeAll() {
	var len = $tabs.tabs('tabs').length;
	$confirm('确定关闭所有窗口？',function() {
		for(var i = len - 1; i >= 1; i--) $tabs.tabs('close',i);	
	});
}

function openTab(node) {
	if(node.url!=null) {
		var tabs = $tabs.tabs('tabs');
		//var tab = null;
		for(var i = 0; i < tabs.length; i++) {
			if(tabs[i].attr('id') == node.id) return $tabs.tabs('select',i);
		}
		return $tabs.tabs('add',{
			id:node.id,title:node.text,iconCls:node.iconCls,closable:true
			,content:'<iframe class="frame" src="'+node.url+'"></iframe>'
		});
	}
}

/** 退出系统 */
function logout() {
	$confirm('确定注销当前用户？',function() {
		$.post('logout.do',function(data) {
			if(data && data.code > 0) window.location.href = $g.basepath;
		},'json');
	});
}

/**心跳数据*/
function heartbeat() {
	$.ajax({url:"heartbeat.do",type:'post',dataType:'json'
		,beforeSend:function(xhr) {}
		,complete:function(xhr,ts) {}
		,success:function(r){
			if(r.code == -10000) top.location.href = "login.do";
		}
	});
	setTimeout(heartbeat,30000);
}

function userHandler(item) {
	if(item&&item.name) {
		switch(item.name) {
		case 'msg':return;
		case 'task':return;
		case 'info':return show({uri:'/app/user/mine.do',title:'我的个人资料',iconCls:'icon-user',width:600,height:380});
		case 'pswd':return show({uri:'/app/user/pswd.do',title:'修改密码',iconCls:'icon-pswd',width:600,height:250});
		case 'logout': return logout();
		}
	}
}
